package com.echat.elocation.receiver;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.echat.elocation.constant.Constants;
import com.echat.elocation.constant.UserInfoKey;
import com.echat.elocation.service.TerminalService;
import com.echat.elocation.util.Logger;
import com.echat.elocation.util.SharedPreferencesUtils;

import java.util.List;

import static com.echat.elocation.constant.Constants.ACCOUNT_SL_ACCOUNT_READ;
import static com.echat.elocation.constant.Constants.ACCOUNT_SL_ACCOUNT_READ_RESULT;

/**
 *
 * @author lihao
 *
 * @date 2018/7/11 9:57
 */
public class KeepAliveReceiver extends BroadcastReceiver {

    private static final String TAG = "KeepAliveReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
            if(Constants.userSlAccount(Build.MODEL)) {
                context.sendBroadcast(new Intent(ACCOUNT_SL_ACCOUNT_READ));
            } else {
                startTerminalService(context);
            }
        }
        // 使用善里账号作为定位账号
        if(ACCOUNT_SL_ACCOUNT_READ_RESULT.equals(intent.getAction())){
            String account = intent.getStringExtra("value");
            Logger.i(TAG, "终端善里账号：" + account);
            int minLength = 10;
            if(account != null && account.length() > minLength){
                String jt808Account = account.substring(1);
                Logger.i(TAG, "定位账号：" + jt808Account);
                SharedPreferencesUtils.put(context, UserInfoKey.SERVER_HOST_KEY, UserInfoKey.SERVER_HOST_DEFAULT);
                SharedPreferencesUtils.put(context, UserInfoKey.SERVER_PORT_KEY, UserInfoKey.SERVER_PORT_DEFAULT);
                SharedPreferencesUtils.put(context, UserInfoKey.PHONE_NUMBER_KEY, jt808Account);
                SharedPreferencesUtils.put(context, UserInfoKey.TERMINAL_ID_KEY, jt808Account);
                SharedPreferencesUtils.put(context, UserInfoKey.CAR_NUMBER_KEY, jt808Account);
                SharedPreferencesUtils.put(context, UserInfoKey.MIN_TIME_KEY, 5);
                SharedPreferencesUtils.put(context, UserInfoKey.IS_SAVE_KEY, true);
                startTerminalService(context);
            }
        }
    }

    private void startTerminalService(Context context) {
        boolean isRunning = isRunningService(context, TerminalService.class.getSimpleName());
        if(!isRunning){
            Intent intent = new Intent(context, TerminalService.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(intent);
        }
    }

    private boolean isRunningService(Context context, String serviceName){
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = manager.getRunningServices(30);
        if(serviceList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < serviceList.size(); i++) {
            if(serviceName.equals(serviceList.get(i).service.getClassName())){
                return true;
            }
        }
        return false;
    }
}
